SetGfxDriver 1:Graphics 340,200,8,2:SeedRnd MilliSecs() :Viewport 0,0,320,300:SetBuffer FrontBuffer() 
Global Arial,nArial,timer,nums,numz,numx
timer = CreateTimer(100) :nums = CreateBank(258):numz= CreateBank(16):numx= CreateBank(16)
For i = 0 To 255 
t = Rand(0,15):PokeByte nums,i, t:PokeByte numz,t, PeekByte (numz,t) +1 
Next 
cnt=0:z=0:z1=0:stopflag=True:stopflag1=False:prestopflag1=False:counter=0:current=0:iterated=0:frames=0


While Not KeyHit(1) 
If cnt<1000 Then
Cls  
set256257()
stat(iterated,z,frames)

For i1 = 0 To 15
For i = 0 To 15
If ((i1*16+i >=z And i1*16+i <=z1)Or (i1*16+i<=z1 And z>=z1) Or (i1*16+i>=z And z>=z1) )Then Color 0,255,0 Else Color 0,155,0
Text 9+18+i*18,18+i1*9,PeekByte (nums,i+i1*16),False ,False 
Color 255,0,0

If (i1*16+i=z) Then
 Rect 9+18+i*18,18+i1*9,19,10,0
current = PeekByte (nums,i1*16+i)
If PeekByte (nums,i1*16+i)<PeekByte (nums,i1*16+i+1) a1 = 0 Else a1=1
If PeekByte (nums,i1*16+i)<PeekByte (nums,i1*16+i+2) a2 = 0 Else a2=1
If PeekByte (nums,i1*16+i+1)<PeekByte (nums,i1*16+i+2) a3 = 0 Else a3=1
If PeekByte (nums,i1*16+i)=PeekByte (nums,i1*16+i+1) a1 = 2
If PeekByte (nums,i1*16+i)=PeekByte (nums,i1*16+i+2) a2 = 2
If PeekByte (nums,i1*16+i+1)=PeekByte (nums,i1*16+i+2) a3 = 2
Text 320,18+i1*9,a1+" "+a2+" "+ a3,False ,False 
Text 355,18+i1*9,current,False ,False 
EndIf

Color 255,255,0
If (i1*16+i=z1) Then
 Rect 9+18+i*18,18+i1*9,19,10,0
If current<PeekByte (nums,i1*16+i+1) za1 = 0 Else za1=1
If current<PeekByte (nums,i1*16+i+2) za2 = 0 Else za2=1
If PeekByte (nums,i1*16+i+1)<PeekByte (nums,i1*16+i+2) za3 = 0 Else za3=1
If current=PeekByte (nums,i1*16+i+1) za1 = 2
If current=PeekByte (nums,i1*16+i+2) za2 = 2
If PeekByte (nums,i1*16+i+1)=PeekByte (nums,i1*16+i+2) za3 = 2
Text 320,18+i1*9,za1+" "+za2+" "+ za3,False ,False 
EndIf

Next
Next




If(a1=za1 And a2=za2 And a3=za3 And counter >0) Then


Select True 
Case prestopflag1 = True 
   stopflag1 = True
  frames=frames+1
;    label1(z,z1)
 ;   Flip
    current = PeekByte (nums,z)
  If z<255 Then 
    For i = z To 254 
    PokeByte nums,i,PeekByte (nums,i+1) 
    Next 
  EndIf 
  PokeByte nums,255,PeekByte (nums,0)
  For i = 0 To z1-1 
      PokeByte nums,i,PeekByte (nums,i+1) 
  Next 
  PokeByte nums,z1,current
    current = PeekByte (nums,z1+1)
  z=z1+1:z1=z1+2
    While (PeekByte (nums,z)<8)
       z=z+1:z1=z1+1
       If (z>255) Then z=0:frames= frames+1
       If (z1>255) Then z1=0:prestopflag1 = True
    Wend
    If z1>255 Then z1=z1-255
  stopflag1 = False
    prestopflag1 =False 
  counter =0
  iterated=iterated+1

Default
   stopflag1 = True
    For i = z To z1-1 
       PokeByte nums,i, PeekByte (nums,i+1) 
    Next 
    PokeByte nums,z1,current
    current = PeekByte (nums,z1+1)
    z=z1+1:z1=z1+2
    While (PeekByte (nums,z)<8)
       z=z+1:z1=z1+1
       If (z>255) Then z=0:frames= frames+1
       If (z1>255) Then z1=0:prestopflag1 = True
    Wend
    If z1>255 Then z1=0
    stopflag1 = False
    counter =0
    iterated=iterated+1
End Select 

EndIf

If (stopflag1 =False)
 If (z1>255) Then z1=0:prestopflag1 = True: label1(z,z1): Else z1=z1+1 
  
EndIf 

counter = counter +1
 

If z>255 Then z=0;cnt=cnt+1: 
;Else
;If stopflag <>True Then
; z = z+1
While (PeekByte(nums,z)<8)
z=z+1:z1=z1+1
If (z>255) Then z=0:frames= frames+1
If (z1>255) Then z1=0:prestopflag1 = True
Wend

;EndIf 
EndIf
label1(z,z1)
Flip 
;WaitKey()
Wend  
EndGraphics 
FreeBank nums:FreeBank numz:FreeBank numx

Function stat(iterated,z,frames)
Text 20,180,iterated+" iterations, "+z+" pointer, "+frames+" frames",False ,False 
End Function

Function set256257()
PokeByte nums,256, PeekByte (nums,0):PokeByte nums,257, PeekByte (nums,1)
End Function



Function label1(z,z1)
For i = 0 To 15
   Text 9,18+i*7,PeekByte (numz,i),False ,False
   PokeByte numx,i,0
Next
cnt=0
For i = 0 To 15
 For i1 = 15 To 0 Step -1
    If (PeekByte (nums,cnt)>7 )Then Color 0,255,0 Else Color 0,o,0
    PokeByte numx,PeekByte (nums,i+i1*16), PeekByte (numx,PeekByte (nums,i+i1*16)) +1 
    Plot i,i1
cnt=cnt+1
    ;Text 9+18+i*7,18+i1*7,Chr$(PeekByte (nums,i+i1*16)),False ,False 
 Next
Next

Color 0,255,0
For i = 0 To 15
If (PeekByte (numx,i)<>PeekByte (numz,i)) Color 255,0,0:WaitKey(): Else Color 0,255,0 
Text 0,18+i*7,PeekByte (numx,i),False ,False
Next


End Function